package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.EsChangeProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.EsChangeFormMapper;
import com.ruoyi.system.domain.EsChangeForm;
import com.ruoyi.system.service.IEsChangeFormService;

/**
 * 换货单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-18
 */
@Service
public class EsChangeFormServiceImpl implements IEsChangeFormService
{
    @Autowired
    private EsChangeFormMapper esChangeFormMapper;

    /**
     * 查询换货单
     *
     * @param id 换货单主键
     * @return 换货单
     */
    @Override
    public EsChangeForm selectEsChangeFormById(Long id)
    {
        return esChangeFormMapper.selectEsChangeFormById(id);
    }

    /**
     * 查询换货单列表
     *
     * @param esChangeForm 换货单
     * @return 换货单
     */
    @Override
    public List<EsChangeForm> selectEsChangeFormList(EsChangeForm esChangeForm)
    {
        return esChangeFormMapper.selectEsChangeFormList(esChangeForm);
    }

    /**
     * 新增换货单
     *
     * @param esChangeForm 换货单
     * @return 结果
     */
    @Override
    public int insertEsChangeForm(EsChangeForm esChangeForm)
    {
        esChangeForm.setCreateTime(DateUtils.getNowDate());
        return esChangeFormMapper.insertEsChangeForm(esChangeForm);
    }

    /**
     * 修改换货单
     *
     * @param esChangeForm 换货单
     * @return 结果
     */
    @Override
    public int updateEsChangeForm(EsChangeForm esChangeForm)
    {
        return esChangeFormMapper.updateEsChangeForm(esChangeForm);
    }

    /**
     * 批量删除换货单
     *
     * @param ids 需要删除的换货单主键
     * @return 结果
     */
    @Override
    public int deleteEsChangeFormByIds(Long[] ids)
    {
        return esChangeFormMapper.deleteEsChangeFormByIds(ids);
    }

    /**
     * 删除换货单信息
     *
     * @param id 换货单主键
     * @return 结果
     */
    @Override
    public int deleteEsChangeFormById(Long id)
    {
        return esChangeFormMapper.deleteEsChangeFormById(id);
    }

    @Override
    public int auditEsChangeForm(EsChangeForm form) {
        // 验证当前状态是否为待审核(1)
        EsChangeForm exist = this.selectEsChangeFormById(form.getId());
        if (exist.getStatus() != 1) {
            throw new ServiceException("只有待审核状态的单据可以审核");
        }

        // 设置更新字段
        EsChangeForm updateForm = new EsChangeForm();
        updateForm.setId(form.getId());
        updateForm.setStatus(form.getStatus());
        updateForm.setRejectReason(form.getRejectReason());
        updateForm.setAuditPersonId(form.getAuditPersonId());
        updateForm.setAuditPersonName(form.getAuditPersonName());
        updateForm.setAuditTime(form.getAuditTime());

        return esChangeFormMapper.updateEsChangeForm(updateForm);
    }

    @Override
    public int submitEsChangeForm(EsChangeForm esChangeForm) {
        // 检查换货单是否存在
        EsChangeForm existing = esChangeFormMapper.selectEsChangeFormById(esChangeForm.getId());
        if (existing == null) {
            throw new ServiceException("换货单不存在");
        }

        // 检查状态是否为未提交(0)
        if (existing.getStatus() != 0) {
            throw new ServiceException("只有未提交状态的换货单可以提交");
        }

        // 更新提交信息
        return esChangeFormMapper.updateEsChangeForm(esChangeForm);
    }

}
